<?php
/**                                          
 * 模版操作页
 * @author M2
 * @version 1.0
 * create Date 2013-07-22
 */
class TemplateAction extends PublicAction {
	
	/**
	 * 站点列表
	 */
	public function sitetemplatelist(){
		$siteList = D('Site')->getSiteListArr('sitetemplatelist');
		$sid = intval($_GET['sid']);
		if ($sid > 0) {
			$data['siteid'] = $sid;
			$list = M('cms_templates')->where($data)->order('id desc')->select();
			$this->assign('list',$list);
			$this->assign('ssid',$sid);
		}
		$this->assign('sitelist', $siteList);
		$this->display();
	}
	
	/**
	 * 将站点列表封装成tree
	 * sun
	 */
	public function siteTreeList() {
		echo D('Site')->getSiteList('sitetemplatelist');
	}
	
	/**
	 * 模版管理	
	 * M2
	 */
	public function tplmanage() {
		$sid = intval($_GET['sid']);
		$data['siteid'] = $sid;
		$list = M('cms_templates')->where($data)->order('id desc')->select();
		$this->assign('list',$list);
		$this->assign('sid',$sid);
		$this->display();
	}
	
	/**
	 * 添加模版页-加载
	 * M2
	 */
	public function addtpl() {
		$sid = intval($_GET['sid']);
		$this->assign('sid',$sid);
		$this->display();
	}
	
	/**
	 * 添加模版页-保存
	 * M2
	 */
	public function doaddtpl() {
		if($_POST) {
			if(!empty($_POST['module']) && !empty($_POST['fieldname'])) {
				if(strpos($_POST['fieldname'],'.'))
					$fieldname = common('Html')->text($_POST['fieldname']);
				else
					$fieldname = common('Html')->text($_POST['fieldname']).'.html';
				
				$sid = intval($_POST['sid']);
				if(!empty($_POST['id'])) {
					
					$data ['id'] = array ('neq',intval($_POST['id']));
				}
				$data ['siteid'] = $sid;
				$data['template_name'] = $fieldname;
				$f = M('cms_templates')->where($data)->find();
				if($f) {
					$this->error('模版名称已存在');
				}
				unset($data);
				$data['siteid'] = $sid;
				$data['template_type'] = $_POST['module'];
				$data['template_name'] = $fieldname;
				$data['create_name'] = common('Html')->text($_POST['create_name']);
				if(!empty($_POST['id'])) {
					$result = false !== M('cms_templates')->where("id={$_POST['id']}")->save($data);
					$result && S('cache_search_templatePath_'.$sid,null);
				} else {
					$result = M('cms_templates')->add($data);
				}
				if($result) {
					$templatePath = D('Site')->getSiteInfo($sid);
					if($data['template_type']=="public"){
						$htmlfile =  $templatePath.'templates/include/'.iconv("UTF-8","GB2312",$fieldname);
					}else{
						$htmlfile =  $templatePath.'templates/'.iconv("UTF-8","GB2312",$fieldname);
					}
					if(!is_dir(dirname($htmlfile)))
						mk_dir(dirname($htmlfile));
					if(false === file_put_contents($htmlfile,stripcslashes($_POST['templateContent']))) {
						$this->error('操作失败'.$htmlfile);
					} else {
						$this->assign('jumpUrl',URL('cms/Template/sitetemplatelist',array('sid'=>$sid)));
						$this->success('操作成功');
					}
				}
			} else {
				$this->error('操作失败');
			}
		}
	}
	
	/**
	 * 修改模版页	
	 * M2
	 */
	public function edittpl() {
		if(!empty($_GET['id'])) {
			$data['id'] = $_GET['id'];
			$sid = intval($_GET['sid']);
			$findTemplate = M('cms_templates')->where($data)->find();
			unset($data);
			if($findTemplate) {
				$templatePath = D('Site')->getSiteInfo($sid);
				if($findTemplate['template_type']=='public'){
					$fieldname =  $templatePath.'templates/include/'.iconv("UTF-8","GB2312",$findTemplate['template_name']);
				}else{
					$fieldname =  $templatePath.'templates/'.iconv("UTF-8","GB2312",$findTemplate['template_name']);
				}
				if(file_exists($fieldname)) {
					$content = file_get_contents($fieldname);
				}
			}
			$content = htmlspecialchars($content, ENT_NOQUOTES, 'UTF-8');
			$findTemplate['templateContent'] = $content;
			$this->assign('sid',$sid);
			$this->assign($findTemplate);
			$this->display();
		}
	}
	
	/**
	 * 删除模板页   
	 * M2
	 */
	public function deletetpl() {
		if (empty ( $_POST ['ids'] )) {
			echo 0;
			exit ();
		}
		$data['id'] = array('in',$_POST['ids']);
		$list = M('cms_templates')->where($data)->select();
		if(empty($list)) {
			echo 0;
			exit();
		}
		$sid = intval($_POST['sid']);
		$templatePath = D('Site')->getSiteInfo($sid);
		foreach ($list as $k => $v) {
			if ($v['template_type']=='public'){
				if(file_exists($templatePath.'templates/include/'.$v['template_name']))
					unlink($templatePath.'templates/include/'.$v['template_name']);
			}else{
				if(file_exists($templatePath.'templates/'.$v['template_name']))
					unlink($templatePath.'templates/'.$v['template_name']);
			}
		}
		echo M('cms_templates')->where($data)->delete() ? 1 : 0;
	}
}

?>